//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by liyue on 15/11/12.
//  Copyright © 2015年 李越. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
//定义图片数量
private let imageCount = 4

class NewFeatureViewController: UICollectionViewController {

    //
    init() {
        //实例化layout
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        //设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    

//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 0
//    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
    //设置背景色
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
        cell.index = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last! as NSIndexPath
        let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
        
        if path.item == imageCount - 1{
            
            cell.startAnimation()
        }
    }
    
//    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        
//        let page = Int(scrollView.contentOffset.x / screenW)
//        if page == imageCount - 1 {
//            
//            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
//                
//                cell.startAnimation()
//            }
//        }
//    }
}

class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
        
        didSet{
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            if index == imageCount - 1 {
                startButton.hidden = false

            } else {
            startButton.hidden = true
            }
        }
    }
    
    @objc private func start() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
//        print("a")
    }
    
    private func startAnimation() {
        
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
//            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        //设置点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
//        startButton.hidden = true
    }
    
    private lazy var iconImage: UIImageView = UIImageView()
    
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
}





